package com.bluedot.www.core.pojo.DTO;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;


/**
 * <p>分页查询结果，数据封装类</p>
 * 使用案例：
 * <pre>
 *   int page= WebUtils.parseInt(req.getParameter("page"),1);
 *         int size= WebUtils.parseInt(req.getParameter("size"),10);
 *         if(page<1){
 *             page=1;
 *         }
 *         String name = req.getParameter("name");
 *         int startRecord=(page-1)*size;
 *         List<Discussion> discussionList=discussService.findAll( startRecord,  size, name);
 *         int recordCount=discussService.getDiscussionNum(name);
 *         Pager<Discussion> pager=new Pager<>();
 *         pager.calculate(discussionList,page,size,recordCount);
 *         req.setAttribute("page",pager);
 * </pre>
 *
 * @author GUOZHIPENG
 * @version 1.0
 * @date 2021/8/8 7:34
 */
public class Pager<T> implements Serializable {

    /**
     * 集合，外界传入
     */
    private List<T> records;

    /**
     * 当前页号，外界传入
     */
    private int currentPage;

    /**
     * 每页记录数，外界可以设定
     */
    private int pageSize;

    /**
     * 总记录条数,外界传入
     */
    private int total;


    public Pager() {
        this.records = Collections.emptyList();
        this.currentPage = 1;
        this.pageSize = 10;
        this.total = 0;
    }

    public Pager(int currentPage, int pageSize) {
        this(currentPage, pageSize, pageSize);
    }

    public Pager(int currentPage, int pageSize, int total) {
        this.pageSize = 10;
        this.total = 0;
        this.records = Collections.emptyList();
        if (currentPage > 0) {
            this.currentPage = currentPage;
        }
        if (pageSize > 0) {
            this.pageSize = pageSize;
        }
        if (total > 0) {
            this.total = total;
        }
    }

    /**
     * 获取limit的第一个参数
     *
     * @return int
     * @author He Peng
     * @date 2021/9/8 9:02
     */
    public int getFirstLimitParam() {
        return (currentPage - 1) * pageSize;
    }

    /**
     * 获取limit的第一个参数
     *
     * @param currentPage: 当前页
     * @param pageSize:    页面大小
     * @return int
     * @author He Peng
     * @date 2021/9/8 14:39
     */
    public int getFirstLimitParam(int currentPage, int pageSize) {
        return (currentPage - 1) * pageSize;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        if (currentPage > 0) {
            this.currentPage = currentPage;
        }
    }

    public List<T> getRecords() {
        return records;
    }

    public void setRecords(List<T> records) {
        this.records = records;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }


    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }


    @Override
    public String toString() {
        return "Pager{" +
                "records=" + records +
                ", currentPage=" + currentPage +
                ", pageSize=" + pageSize +
                ", total=" + total +
                '}';
    }

}
